#pragma once

#include "rtgmodes.h"

// extern int ddforceram;
// extern int useoverlay;

struct ddstuff
{
    int ddinit;
    int ddzeroguid;
    GUID ddguid;
    LPDIRECTDRAW7 maindd;
    // LPDIRECTDRAWCLIPPER dclip;
    LPDIRECTDRAWSURFACE7 primary, secondary, flipping[2];
    // DDOVERLAYFX overlayfx;
    // DWORD overlayflags;
    int fsmodeset, backbuffers;
    int width, height, depth, freq;
    int swidth, sheight;
    DDSURFACEDESC2 native;
    DDSURFACEDESC2 locksurface;
    // int lockcnt;
    DWORD pitch;
    HWND hwnd;
    uint colorkey;
    // int islost, isoverlay;

    LPDIRECTDRAWSURFACE7 statussurface;
    int statuswidth, statusheight;
};
struct ddcaps
{
    int maxwidth, maxheight;
    // int cancolorkey;
    // int cannonlocalvidmem;
};
extern struct ddstuff dxdata;
extern struct ddcaps dxcaps;

struct ScreenResolution
{
    uint width;  /* in pixels */
    uint height; /* in pixels */
};

#define MAX_PICASSO_MODES 300
#define MAX_REFRESH_RATES 100

struct PicassoResolution
{
    struct ScreenResolution res;
    int depth;   /* depth in bytes-per-pixel */
    int residx;
    int refresh[MAX_REFRESH_RATES]; /* refresh-rates in Hz */
    int refreshtype[MAX_REFRESH_RATES]; /* 0=dx,1=enumdisplaysettings */
    TCHAR name[25];
    /* Bit mask of RGBFF_xxx values.  */
    uint colormodes;
    int nondx;
};

extern GUID* displayGUID;

#define MAX_DISPLAYS 10

struct MultiDisplay
{
    int primary, disabled /*, gdi*/;
    GUID guid;
    TCHAR* name, * name2, * name3;
    struct PicassoResolution* DisplayModes;
    RECT rect;

    MultiDisplay ()
    {
        name = nullptr;
        name2 = nullptr;
        name3 = nullptr;
        primary = 0;
        disabled = 0;
        memset(&guid, 0, sizeof(GUID));
        DisplayModes = nullptr;
        memset(&rect, 0, sizeof(RECT));
    }

    ~MultiDisplay ()
    {
        delete name;
        delete name2;
        delete name3;
        delete DisplayModes;
    }
};
extern struct MultiDisplay Displays[MAX_DISPLAYS];

enum DirectDraw_Mask_e
{
    red_mask,
    green_mask,
    blue_mask
};

extern const TCHAR* DXError(HRESULT hr);
extern TCHAR* outGUID(const GUID* guid);

HRESULT DirectDraw_GetDisplayMode();
void DirectDraw_Release();
int DirectDraw_Start(GUID* guid);
// void clearsurface(LPDIRECTDRAWSURFACE7 surf);
// int locksurface(LPDIRECTDRAWSURFACE7 surf, LPDDSURFACEDESC2 desc);
// void unlocksurface(LPDIRECTDRAWSURFACE7 surf);
// HRESULT restoresurface(LPDIRECTDRAWSURFACE7 surf);
// LPDIRECTDRAWSURFACE7 allocsurface(int width, int height);
// LPDIRECTDRAWSURFACE7 allocsystemsurface(int width, int height);
// LPDIRECTDRAWSURFACE7 createsurface(byte* ptr, int pitch, int width, int height);
// void freesurface(LPDIRECTDRAWSURFACE7 surf);
void DirectDraw_FreeMainSurface();
// HRESULT DirectDraw_CreateMainSurface(int width, int height);
// HRESULT DirectDraw_SetDisplayMode(int width, int height, int bits, int freq);
// HRESULT DirectDraw_SetCooperativeLevel(HWND window, int fullscreen, int doset);
// HRESULT DirectDraw_CreateClipper();
// HRESULT DirectDraw_SetClipper(HWND hWnd);
RGBFTYPE DirectDraw_GetSurfacePixelFormat(LPDDSURFACEDESC2 surface);
HRESULT DirectDraw_EnumDisplayModes(DWORD flags, LPDDENUMMODESCALLBACK2 callback, void* context);
HRESULT DirectDraw_EnumDisplays(LPDDENUMCALLBACKEXA callback);
DWORD DirectDraw_CurrentWidth();
DWORD DirectDraw_CurrentHeight();
DWORD DirectDraw_GetCurrentDepth();
// int DirectDraw_SurfaceLock();
// void DirectDraw_SurfaceUnlock();
// byte * DirectDraw_GetSurfacePointer();
// DWORD DirectDraw_GetSurfacePitch();
// int DirectDraw_IsLocked();
DWORD DirectDraw_GetPixelFormatBitMask(DirectDraw_Mask_e mask);
RGBFTYPE DirectDraw_GetPixelFormat();
// DWORD DirectDraw_GetBytesPerPixel();
// HRESULT DirectDraw_GetDC(HDC* hdc);
// HRESULT DirectDraw_ReleaseDC(HDC hdc);
// int DirectDraw_GetVerticalBlankStatus();
DWORD DirectDraw_CurrentRefreshRate();
// void DirectDraw_GetPrimaryPixelFormat(DDSURFACEDESC2* desc);
// HRESULT DirectDraw_FlipToGDISurface();
// int DirectDraw_Flip(int doflip);
// int DirectDraw_BlitToPrimary(RECT* rect);
// int DirectDraw_BlitToPrimaryScale(RECT* dstrect, RECT* srcrect);
// int DirectDraw_Blit(LPDIRECTDRAWSURFACE7 dst, LPDIRECTDRAWSURFACE7 src);
// int DirectDraw_BlitRect(LPDIRECTDRAWSURFACE7 dst, RECT* dstrect, LPDIRECTDRAWSURFACE7 src, RECT* scrrect);
// int DirectDraw_BlitRectCK(LPDIRECTDRAWSURFACE7 dst, RECT* dstrect, LPDIRECTDRAWSURFACE7 src, RECT* scrrect);
// void DirectDraw_FillSurface(LPDIRECTDRAWSURFACE7 dst, RECT* rect, uint color);
// void DirectDraw_Fill(RECT* rect, uint color);
// void DirectDraw_FillPrimary();
// bool DirectDraw_vblank_busywait();
// bool DirectDraw_waitvblankstate(bool);

// void dx_check();
// int dx_islost();

// #define DDFORCED_NONLOCAL 0
// #define DDFORCED_DEFAULT 1
// #define DDFORCED_VIDMEM 2
// #define DDFORCED_SYSMEM 3

// #endif

// if (mask2texture)
// {
//     D3DXMATRIXA16 t;
//     RECT r;
//     float srcw = mask2texture_w;
//     float srch = mask2texture_h;
//     float aspectsrc = srcw / srch;
//     float aspectdst = (float)window_w / window_h;
//     float w, h;
//
//     w = mask2texture_multx;
//     h = mask2texture_multy;
//     #if 0
//     if (g_curr_conf.gfx_filteroverlay_pos.width > 0)
//         w = (float)g_curr_conf.gfx_filteroverlay_pos.width / srcw;
//     else if (g_curr_conf.gfx_filteroverlay_pos.width == -1)
//         w = 1.0;
//     else if (g_curr_conf.gfx_filteroverlay_pos.width <= -24000)
//         w = w * (-g_curr_conf.gfx_filteroverlay_pos.width - 30000) / 100.0;
//
//     if (g_curr_conf.gfx_filteroverlay_pos.height > 0)
//         h = (float)g_curr_conf.gfx_filteroverlay_pos.height / srch;
//     else if (g_curr_conf.gfx_filteroverlay_pos.height == -1)
//         h = 1;
//     else if (g_curr_conf.gfx_filteroverlay_pos.height <= -24000)
//         h = h * (-g_curr_conf.gfx_filteroverlay_pos.height - 30000) / 100.0;
//     #endif
//     MatrixScaling(&t, w, h, 0);
//
//     v.x = 0;
//     if (g_curr_conf.gfx_filteroverlay_pos.x == -1)
//         v.x = (window_w - (mask2texture_w * w)) / 2;
//     else if (g_curr_conf.gfx_filteroverlay_pos.x > -24000)
//         v.x = g_curr_conf.gfx_filteroverlay_pos.x;
//     else
//         v.x = (window_w - (mask2texture_w * w)) / 2 + (-g_curr_conf.gfx_filteroverlay_pos.x - 30100) * window_w / 100.0;
//
//     v.y = 0;
//     if (g_curr_conf.gfx_filteroverlay_pos.y == -1)
//         v.y = (window_h - (mask2texture_h * h)) / 2;
//     else if (g_curr_conf.gfx_filteroverlay_pos.y > -24000)
//         v.y = g_curr_conf.gfx_filteroverlay_pos.y;
//     else
//         v.y = (window_h - (mask2texture_h * h)) / 2 + (-g_curr_conf.gfx_filteroverlay_pos.y - 30100) * window_h / 100.0;
//
//     v.x /= w;
//     v.y /= h;
//     v.x = v.y = 0;
//     v.z = 0;
//     v.x += mask2texture_offsetw / w;
//
//     r.left = 0;
//     r.top = 0;
//     r.right = mask2texture_w;
//     r.bottom = mask2texture_h;
//
//     sprite->SetTransform(&t);
//     sprite->Draw(mask2texture, &r, nullptr, &v, 0xffffffff);
//     sprite->Flush();
//     MatrixScaling(&t, 1, 1, 0);
//     sprite->SetTransform(&t);
//
//     if (mask2texture_offsetw > 0)
//     {
//         v.x = 0;
//         v.y = 0;
//         r.left = 0;
//         r.top = 0;
//         r.right = mask2texture_offsetw + 1;
//         r.bottom = window_h;
//         sprite->Draw(blanktexture, &r, nullptr, &v, 0xffffffff);
//         if (window_w > mask2texture_offsetw + mask2texture_ww)
//         {
//             v.x = mask2texture_offsetw + mask2texture_ww;
//             v.y = 0;
//             r.left = 0;
//             r.top = 0;
//             r.right = window_w - (mask2texture_offsetw + mask2texture_ww) + 1;
//             r.bottom = window_h;
//             sprite->Draw(blanktexture, &r, nullptr, &v, 0xffffffff);
//         }
//     }
//
// }